package com.gaohe.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gaohe.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//用于公共字段的填充
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    //通过httpSession来拿
//    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//    Employee employee = (Employee) request.getSession().getAttribute("employee");
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
